package com.amnc.client.threadPackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;

import com.amnc.common.dif.DIFSheet;
import com.amnc.common.dif.exception.DIFDataException;
import com.amnc.common.dif.exception.DIFDataPairParsingException;
import com.amnc.common.dif.exception.DIFKeywordException;
import com.amnc.common.dif.exception.DIFNumberPairInfoException;
import com.amnc.common.dif.exception.DIFStringFormatException;
import com.amnc.common.util.SendDifData;
import com.amnc.model.AbortionPO;

public class AbortionThread02 implements Runnable{
	private AbortionPO abortionPO=new AbortionPO(); 
	private String newPath="D:\\D1";
	

	File flagFileProcessing=null;	//文件发送标识
	//File flagFileFinished=null;	//文件标识
	File file=null;
	String repeat="0";	//是否是重新发送的 标识，用于服务端;“0”代表第一次发送，“1”代表重复发送。
	boolean isFirst=true; //是否是第一次发送
	
	private DIFSheet sheet;
	private  int columnMax=6;
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while (true) {
				open(newPath);
				sendAbortion(sheet,columnMax, this.repeat);
				flagFileProcessing.delete();
			}
			
		} catch (DIFKeywordException | DIFDataPairParsingException
				| DIFNumberPairInfoException | DIFStringFormatException
				| IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DIFDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 在指定发送目录中读取文件
	 * @param newPath
	 * @throws IOException
	 * @throws DIFKeywordException
	 * @throws DIFDataPairParsingException
	 * @throws DIFNumberPairInfoException
	 * @throws DIFStringFormatException
	 * @throws ParseException
	 */
	public void open(String newPath) throws IOException, DIFKeywordException, DIFDataPairParsingException, DIFNumberPairInfoException, DIFStringFormatException, ParseException{
		  File f=new File(newPath);
		  int index,indexProcessing;
	      File fileList[]=f.listFiles();
	      //判断 流产dif文件及其标识文件是否都存在，都存在则是重复发送，将isSetRepeat设为true；只有dif文件，则是第一次发送；
	      for (int i = 0; i < fileList.length; i++) {
	    	  //既存在 “流产” 又存在  “processing”，即存在发送标识文件
	    	  if (fileList[i].toString().indexOf("流产")!=-1 && fileList[i].toString().indexOf("processing")!=-1 ) {
				isFirst=false;
				break;
			}
		}
	      
	      if (isFirst) {//进行第一次读取   第一次发送
	    	  for (int i = 0; i < fileList.length; i++) {
		    	   index=fileList[i].toString().indexOf("流产");
		    	   indexProcessing=fileList[i].toString().indexOf("processing");
		    	   //存在“流产”文件，可以发送,并且是第一次发送,生成处理标识文件 “文件名+.processing”
		    	   if (index!=-1 && indexProcessing==-1) {
		    		   this.flagFileProcessing=new File(fileList[i].toString()+".processing");
		    		   flagFileProcessing.createNewFile();
					InputStream inputStream = new FileInputStream(fileList[i]);
					this.sheet = new DIFSheet(inputStream);
					System.out.println(this.sheet);
					break;
				}
			}
		}else {//进行重复读取  重复发送
			for (int i = 0; i < fileList.length; i++) {
		    	   index=fileList[i].toString().indexOf("流产");
		    	   indexProcessing=fileList[i].toString().indexOf("processing");
		    	   if (index!=-1 && indexProcessing==-1) {//找到含有“流产”字符的 流产dif文件
		    		this.flagFileProcessing=new File(fileList[i].toString()+".processing");
					InputStream inputStream = new FileInputStream(fileList[i]);
					this.sheet = new DIFSheet(inputStream);
					System.out.println(this.sheet);
					break;
				}
			}
			this.repeat="1";//告知服务端agent在进行重复发送，服务端需要再进行验证是否已经存在相同记录
		}	
		
	}
	
	
	public void sendAbortion(DIFSheet sheet,int columnMax,String repeat) throws DIFDataException{
		String[] rows = new String[columnMax + 1];
		int rowsN = sheet.getRows().size();
		for (int i = 2; i < rowsN; i++) {
			for (int j = 1; j <= columnMax; j++) {
				String cellValue = sheet.getRow(i).getCell(j)
						.getStringCellValue();
				rows[j] = cellValue;
			}

			// 流产日期不能为空
			if (!"".equals(rows[3]) && rows[3] != null) {
				abortionPO.setFarmId(rows[1].toString());
				abortionPO.setCowId(rows[2].toString());
				abortionPO.setDate(rows[3]);
				abortionPO.setReason(rows[4].toString());
				abortionPO.setComment(rows[5].toString());
				abortionPO.setResponsibleName(rows[6].toString());
				abortionPO.setRepeat(repeat);
				

//				String url = "http://192.168.1.2:8080/amnc-integration-afifarm-1.0-SNAPSHOT/event/abortion";
				String url = "http://localhost:8080/amnc-integration-afifarm-1.0-SNAPSHOT/event/abortion";
				SendDifData<AbortionPO> sendDifData=new SendDifData<AbortionPO>();
				sendDifData.sendData(abortionPO, url);
				
			}

		}
	}
	
	
	
	
	
	
	
	
	
	
}
